<template>
  <div id="video" v-if="isShow">
    <div class="close" @click="closePane">×</div>
    <div class="title">{{ name }}</div>
    <div class="content">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="videoList">
            <div style="font-size:30px">周边摄像头</div>
            <hr style="margin-bottom:30px"/>
            <div
              class="videoListItem"
              v-for="(item, index) in videoDataList"
              :key="index"
              :class="{ selected: tabIndex == index }"
              @click="handelClick(item, index)"
            >
              {{ item.name }}
            </div>
          </div>
        </el-col>
        <el-col :span="18">
          <video
            muted
            controls
            class="video"
            ref="video"
            style="width: 100%; height: 100%"
          ></video>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import { videoUrl, eneryEquipmentQuery } from "../../utils/api";
let Hls = require("hls.js");
export default {
  data() {
    return {
      name: "---",
      isShow: false,
      hls: null,
      tabIndex: '',
      videoDataList: [
        { name: "1F_电梯按键_01", code: "" },
        { name: "1F_电梯按键_02", code: "" },
        { name: "1F_电梯按键_03", code: "" },
        { name: "1F_电梯按键_04", code: "" },
        { name: "1F_半球摄像头_01", code: "" },
        { name: "1F_半球摄像头_02", code: "" },
        { name: "1F_半球摄像头_03", code: "" },
        { name: "1F_半球摄像头_04", code: "" },
      ],
    };
  },
  mounted() {
    window.showHelp = this.help;
  },
  methods: {
    // 接收传送过来的信息
    help(val) {
      this.isShow = val.show;
      this.name = val.title;
    },
    // 关闭弹窗
    closePane() {
      this.isShow = false;
      this.videoPause();
    },
    getStream(source) {
      if (Hls.isSupported()) {
        this.hls = new Hls();
        this.hls.loadSource(source);
        this.hls.attachMedia(this.$refs.video);

        this.hls.on(Hls.Events.MANIFEST_PARSED, () => {
          console.log("视频加载成功");
          this.$refs.video.play();
        });
        this.hls.on(Hls.Events.ERROR, (event, data) => {
          console.log("视频加载失败");
        });
      } else if (
        this.$refs.video.canPlayType("application/vnd.apple.mpegurl")
      ) {
        this.$refs.video.src =
          "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8";
        this.$refs.video.addEventListener("loadedmetadata", function () {
          this.$refs.video.play();
        });
      }
    },
    // 停止流
    videoPause() {
      if (this.hls) {
        this.$refs.video.pause();
        this.hls.destroy();
        this.hls = null;
      }
    },
    // 视频列表点击
    handelClick(item, index) {
      this.tabIndex = index;
      this.name = item.name;
      this.getCode(item.name);
    },
    getCode(info) {
      eneryEquipmentQuery().then((response) => {
        var alldata = response.data.data;
        var clickVideo = alldata.filter((item) => {
          return item.code && item.code == info;
        });
        // this.getStream('http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8')
        var param =
          "?cameraIndexCode=" +
          clickVideo[0].cameraIndexCode +
          "&protocol=hls&isTest=0";
        videoUrl(param).then((res) => {
          this.getStream(res && res.data && res.data.data && res.data.data.url);
        });
      });
    },
  },
};
</script>
<style scoped>
#video {
  margin: 0 auto;
  box-sizing: border-box;
  position: absolute;
  left: 31%;
  top: 520px;
  width: 1490px;
  height: 900px;
  color: white;
  background: url("../../../static/images/popups/detailsPane.png") !important;
  background-position: center !important;
  background-repeat: no-repeat !important;
  background-size: 100% 100% !important;
  transition: all 1.2s;
}
/* 右上角关闭按钮 */
.close {
  float: right;
  font-size: 50px;
  font-weight: 900;
  margin-right: 30px;
  margin-top: 10px;
  transition: all 1s;
}
.close:hover {
  transform: scale(1.1);
  color: red;
  cursor: pointer;
}
.content {
  box-sizing: border-box;
  width: 100%;
  height: 800px;
  padding: 80px;
  margin-top: 30px;
}
.title {
  position: absolute;
  left: 40px;
  top: 30px;
  font-size: 32px;
  font-weight: 800;
}
.videoList {
  max-height: 740px;
  overflow: auto;
}
.videoListItem {
  box-sizing: border-box;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color: rgb(117, 169, 228);
  margin: 10px 0;
  padding-left: 20px;
  font-size: 25px;
  border-radius: 4px;
}
.videoListItem:hover {
  background-color: rgb(7, 113, 184);
  cursor: pointer;
}
.selected {
  background-color: rgb(7, 113, 184);
}
</style>